# !/usr/bin/python
# -*- coding=utf-8 -*-
"""
user admin
"""
from django.contrib import admin
from django.contrib.auth.forms import UserCreationForm
from django.utils.translation import gettext_lazy as _

from user.models import Role
from user.models import User


@admin.register(Role)
class RoleAdmin(admin.ModelAdmin):
    """
    角色
    """
    list_v_display = ['name', 'description', 'create_time', 'update_time']
    fieldsets = (
        (None, {'fields': ('name', 'key', 'description')}),
    )


@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('email', 'phone')}),
        (_('Permissions'), {'fields': (  # 'is_active', 'is_staff',
            'is_superuser',
            'groups',)}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'password1', 'password2'),
        }),
    )
    add_form = UserCreationForm
    ordering = ['order']

    list_display = ('username', 'name', 'sex', 'email', 'phone', 'is_active', 'del_flag')
    list_filter = ['sex', 'is_active', 'role', 'groups']
    search_fields = ['username', 'name', 'phone']
    filter_horizontal = ('user_permissions',)
    readonly_fields = ("password",)
    # v_change_form_add = "v_user_add_view/"
    exclude = ('user_permissions',)

    def get_fieldsets(self, request, obj=None):
        if not (obj and obj.pk):
            return self.add_fieldsets

        fieldsets = (
            (None, {'fields': ('username', 'password')}),
            (_('Personal info'), {'fields': ('name', 'sex', 'email', 'phone', 'order')}),
            (_('Permissions'), {'fields': ('is_active', 'is_staff',
                                           'is_admin',
                                           'is_superuser',
                                           'del_flag',
                                           'groups',)}),
            (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
        )
        return fieldsets
